/*
 * Created on 2005-4-20
 * 
 * Copyright bloomsoft.cn All rights reserved
 */
package org.nanhill.commons.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public abstract class FileKit {
	public static void copy(File infile, File outfile) throws IOException {
		InputStream in = new FileInputStream(infile);
		OutputStream out = new FileOutputStream(outfile);
		IoKit.copy(in, out);
		in.close();
		out.close();
	}

	/**
	 * @param infile
	 * @param outfile
	 * @param capacity
	 * @throws IOException
	 * @deprecated
	 */
	public static void copy(File infile, File outfile, int capacity)
			throws IOException {
		FileInputStream fsin = new FileInputStream(infile);
		FileOutputStream fsout = new FileOutputStream(outfile);
		try {
			copy(fsin, fsout, capacity);
		} finally {
			fsout.close();
			fsin.close();
		}
	}

	/**
	 * @param is
	 * @param os
	 * @throws IOException
	 * @deprecated
	 */
	public static void copy(FileInputStream is, FileOutputStream os)
			throws IOException {
		copy(is, os, 1024);
	}

	/**
	 * TODO test only
	 * 
	 * @param is
	 * @param os
	 * @param capacity
	 * @throws IOException
	 * @deprecated
	 */
	public static void copy(FileInputStream is, FileOutputStream os,
			int capacity) throws IOException {
		FileChannel fcin = is.getChannel();
		FileChannel fcout = os.getChannel();
		try {
			ByteBuffer buffer = ByteBuffer.allocate(capacity);
			while (true) {
				buffer.clear();
				int r = fcin.read(buffer);
				if (r == -1) {
					break;
				}
				buffer.flip();
				fcout.write(buffer);
			}
		} finally {
			fcout.close();
			fcin.close();
		}
	}

	public static String getFileName(Class clz, String fileName) {
		URL url = clz.getResource(fileName);
		if (null == url) {
			return null;
		}
		return url.getFile();
	}

	public static File getFile(Class clz, String fileName) {
		String file = getFileName(clz, fileName);
		if (null == file) {
			return null;
		}
		return new File(file);
	}

	/**
	 * get file accociate with the input class, if file is not exist, create and
	 * return it
	 * 
	 * @param clz
	 * @param fileName
	 * @return
	 * @throws IOException
	 *             if file not exist and can't create new file.
	 */
	public static File getFileFirmly(Class clz, String fileName)
			throws IOException {
		File file = getFile(clz, fileName);
		if (null == file) {
			if (createNewFile(clz, fileName)) {
				return getFile(clz, fileName);
			} else {
				throw new IOException("Can't create new file.");
			}
		} else {
			return file;
		}
	}

	public static boolean createNewFile(Class clz, String fileName)
			throws IOException {
		URL url = clz.getResource("");
		String path = url.getPath() + fileName;
		File desFile = new File(path);
		return desFile.createNewFile();
	}

	public static String getClassRootFilename(Class clz) {
		return clz.getResource("/").getFile();
	}

	public static String getClassPackageFilename(Class clz) {
		return clz.getResource("").getFile();
	}

	public static void writeFile(String filename, String content)
			throws IOException {
		BufferedWriter out = new BufferedWriter(new FileWriter(filename));
		out.write(content);
		out.close();

	}

	public static void createNewDoc(String filename) throws IOException {
		File file = new File(filename);
		if (!file.exists()) {
			// Create file if it does not exist
			boolean success = file.mkdirs();
			if (!success) {
				throw new IOException("create file doc error: " + filename);
			}
		}
	}

	public static void createNewFile(String filename) throws IOException {
		File file = new File(filename);
		if (!file.exists()) {
			// Create file if it does not exist
			boolean success = file.createNewFile();
			if (!success) {
				throw new IOException("create file error: " + filename);
			}
		}
	}

	public static void removeFile(String filename) throws IOException {
		File file = new File(filename);
		if (file.exists()) {
			boolean success = file.delete();
			if (!success) {
				throw new IOException("delete file error: " + filename);
			}
		}
	}

	public static void convertFileEncoding(String fromEncoding,
			String toEncoding, File fromFile, File toFile) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(
				new FileInputStream(fromFile), fromEncoding));
		if (!toFile.exists()) {
			toFile.createNewFile();
		}
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(toFile), toEncoding));
		for (String str = in.readLine(); str != null; str = in.readLine()) {
			out.write(str);
			out.write("\n");
		}
		in.close();
		out.close();
	}

}
